Page({
  data: {
    dialogVisible: false,
    houseDataList: [],
    id: '',
  },

  swipeClose(ev) {
    const {
      position,
      instance
    } = ev.detail
    const id = ev.currentTarget.dataset.id

    if (position === 'right') {
      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true,
        id,
      })

      // swiper-cell 滑块关闭
      instance.close()
    }
  },

  async dialogClose(e) {
    if (e.detail === 'confirm') {
      const id = this.data.id

      await wx.http.delete(`/room/${id}`)
      wx.utils.toast('删除成功')
      await this.getHouseDataList()
    }
  },

  goDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/house_pkg/pages/detail/index?id=${id}`,
    })
  },

  addHouse() {
    if (this.data.houseDataList.length >= 6) return wx.utils.toast('当前房屋数量已满六个')
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },

  async getHouseDataList() {
    const {
      data
    } = await wx.http.get('/room')
    this.setData({
      houseDataList: data,
    })
  },
  async onShow() {
    await this.getHouseDataList()
  },
})